<?php
/**
 * 关于wordpress文章信息类
 * Author: Midoks
 * Author URI: http://midoks.cachecha.com/
 */
class weixin_wordpress{

	public $obj;
	public $cmd;
	//public $wpdb;

	//架构函数
	public function __construct($obj, $cmd){
		//global $wpdb;
		//$this->wpdb = $wpdb;
		$this->obj = $obj;
		$this->cmd = $cmd;
	}

	/**
	 * @func 最新5篇文章信息
	 */
	/*public function new5(){
		global $wpdb;
		$sql = "SELECT post_title,guid from {$wpdb->posts}
		   		where post_status='publish' order by id desc limit 5";

		$res = $wpdb->get_results($sql);
		$info = array();
		foreach($res as $k=>$v){
			if($k==0){
				$a['title'] = $v->post_title;
				$a['desc'] = $v->post_title;
				$a['pic'] = $this->obj->bigPic();
				$a['link'] = $v->guid;
			}else{
				$a['title'] = $v->post_title;
				$a['desc'] = $v->post_title;
				$a['pic'] = $this->obj->smallPic();
				$a['link'] = $v->guid;
			}
			$info[] = $a;
		}	
		return $this->obj->toMsgTextPic($info);
	}*/
	public function new5(){
		query_posts('showposts=5');
		$info = array();
		$i = 0;
		while(have_posts()){the_post();
			++$i;
			if($i==1){
				$a['title'] = get_the_title();
				$a['desc'] = get_the_title();
				$a['pic'] = $this->obj->bigPic();
				$a['link'] = get_permalink();
			}else{
				$a['title'] = get_the_title();
				$a['desc'] = get_the_title();
				$a['pic'] = $this->obj->smallPic();
				$a['link'] = get_permalink();
			}
			$info[] = $a;
		}
		return $this->obj->toMsgTextPic($info);//图文
	}

	/**
	 * @func 最热5篇文章信息
	 */
	public function hot5(){
		query_posts(array(
			'post_status' => 'publish',		//选择公开的文章
			'post_not_in' => array(),		//排除当前文章
			'ignore_sticky_posts'=> 1,		//排除顶置文章
			'orderby' => 'comment_count', 	//依据评论排序
			'showposts' => 5,				//调用的数量
		));
		$info = array();
		$i = 0;
		while(have_posts()){the_post();
			++$i;
			if($i==1){
				$a['title'] = get_the_title();
				$a['desc'] = get_the_title();
				$a['pic'] = $this->obj->bigPic();
				$a['link'] = get_permalink();
			}else{
				$a['title'] = get_the_title();
				$a['desc'] = get_the_title();
				$a['pic'] = $this->obj->smallPic();
				$a['link'] = get_permalink();
			}
			$info[] = $a;
		}
		//wp_reset_query();
		//var_dump($info, $this->obj);
		//return '123';
		return $this->obj->toMsgTextPic($info);//图文
	}

	/**
	 * @func 随机5篇文章信息
	 */
	public function rand5(){
		query_posts('showposts=5&orderby=rand');
		$info = array();
		$i = 0;
		while(have_posts()){the_post();
			++$i;
			if($i==1){
				$a['title'] = get_the_title();
				$a['desc'] = get_the_title();
				$a['pic'] = $this->obj->bigPic();
				$a['link'] = get_permalink();
			}else{
				$a['title'] = get_the_title();
				$a['desc'] = get_the_title();
				$a['pic'] = $this->obj->smallPic();
				$a['link'] = get_permalink();
			}
			$info[] = $a;
		}
		return $this->obj->toMsgTextPic($info);//图文
	}

	//@func 总文章数
	public function total(){
		$arg['posts_per_page'] = -1;
		$query = new WP_Query($arg);
		$total = $query->post_count;
		$page = ceil($total/5);
		$text = '共有:'.$total.'篇文章'."\n";
		$text .= $page.'页(5篇一页)';
		return $this->obj->toMsgText($text);
	}

	//@func 页面浏览
	public function pageView($kw){
		//先判断是否是关键字查询
		if($kq = $this->keyQuery($kw)){
			return $kq;
		}

		$word_prefix = substr($kw, 0, 1);
		if($word_prefix!='p'){
			return false;
		}
		//var_dump($word_prefix);
		$word_suffix = substr($kw, 1);
		//var_dump(is_numeric($word_suffix));
		if(!is_numeric($word_suffix)){
			//var_dump($word_prefix);
			return false;
		}
		//var_dump($word_prefix);

		$arg['posts_per_page'] = -1;
		$query = new WP_Query(array('posts_per_page'=>-1));
		$pageTotal = ceil($query->post_count/5);
		//var_dump($word_suffix,$pageTotal);
		if($word_suffix > $pageTotal){
			return $this->cmd->error("你输入页数太大!!!\n");
		}

		$arg = array(
			'order' => 'DESC',		//ASC升序 DESC降序
			'showposts' => 5,		//获取指定的数据量
			'paged' => $word_suffix,//当前第几页
		);
		query_posts($arg);
		$info = array();
		$i = 0;
		while(have_posts()){the_post();
			++$i;
			if($i==1){
				$a['title'] = get_the_title();
				$a['desc'] = get_the_title();
				$a['pic'] = $this->obj->bigPic();
				$a['link'] = get_permalink();
			}else{
				$a['title'] = get_the_title();
				$a['desc'] = get_the_title();
				$a['pic'] = $this->obj->smallPic();
				$a['link'] = get_permalink();
			}
			$info[] = $a;
		}
		return $this->obj->toMsgTextPic($info);//图文
	}

	//关键字查询
	public function keyQuery($kw){
		//var_dump($kw);
		//$kw = $this->convert($kw);
		//var_dump($kw);
		$word_prefix = substr($kw, 0, 1);
		//var_dump($word_prefix);
		if($word_prefix != '?'){
			return false;
		}
		$word_suffix = substr($kw, 1);
		$keyWord = explode('!', $word_suffix);
		//var_dump($keyWord);
		if(count($keyWord)==1){//关键字前5篇文章
			//return $this->keyWordSoso($keyWord);
			return $this->keyWordSoso($keyWord[0]);
		}
		//询问文章数据
		if($keyWord[1]=='?'){
			return $this->keyWordSoso($keyWord[0], '?');
		}
		//翻页功能
		if(is_numeric($keyWord[1])){
			return $this->keyWordSoso($keyWord[0], $keyWord[1]);
		}
	}

	/**
	 *	@func 关键字搜索(新 5篇)
	 *	@param $key 关键字
	 *	@ret string xml
	 */
	public function keyWordSoso($key, $sign=''){
		//判断$sign
		$res = $this->keySoso($key, $sign);

		if($sign == '?'){
			$num = count($res);
			$p = ceil($num/5);
			return $this->obj->toMsgText("~{$key}~关键字,共{$num}篇有{$p}页!!!\n");
		}
		if(!$res){
			if(empty($sign)){
				return $this->cmd->error("~{$key}~关键字,没有数据!!!\n");
			}
			return $this->cmd->error("~{$key}~关键字,没有第{$sign}页!!!\n");
		}
		//var_dump($res);
		$info = array();
		foreach($res as $k=>$v){
			if($k==0){
				$a['title'] = $v->post_title;
				$a['desc'] = $v->post_title;
				$a['pic'] = $this->obj->bigPic();
				$a['link'] = $v->guid;
			}else{
				$a['title'] = $v->post_title;
				$a['desc'] = $v->post_title;
				$a['pic'] = $this->obj->smallPic();
				$a['link'] = $v->guid;
			}
			$info[] = $a;
		}
		if(count($info)==0){
			return $this->cmd->error("~{$key}~关键字,没有数据!!!");
		}
		return $this->obj->toMsgTextPic($info);
	}

	/**
	 *	@func 关键查询
	 *	@param string $k 关键字
	 *	@ret array
	 */
	public function keySoso($k, $sign){
		global $wpdb;
		$limit = '';
		//limit
		//判断$sign
		if(empty($sign)){
			$limit = 'limit 0, 5';
		}else if($sign=='?'){
			$limit = '';
		}else{
			$p = 5*($sign-1);
			$limit = "limit {$p},5";
		}
	
		/*$sql = "SELECT p.post_title,p.guid from {$wpdb->posts} as p ".
			"inner join {$wpdb->term_relationships} as tr on p.id=tr.term_taxonomy_id ".
			"inner join {$wpdb->term_taxonomy} as tt on tt.term_taxonomy_id=tr.term_taxonomy_id ".
			"inner join {$wpdb->terms} as t on t.term_id=tt.term_id ".
			"where p.post_status='publish' ".
			"order by p.id desc limit 0,5";
		
		$sql2 = "SELECT p.post_title,p.guid,tr.* from {$wpdb->posts} as p ".
			"left join {$wpdb->term_relationships} as tr on p.id=tr.term_taxonomy_id ".
			"left join {$wpdb->term_taxonomy} as tt on tt.term_taxonomy_id=tr.term_taxonomy_id ".
			"left join {$wpdb->terms} as t on t.term_id=tt.term_id ".
			"where p.post_status='publish' ".
			"order by p.id desc limit 0,5";

		$sql3 = "SELECT p.post_title,p.guid from {$wpdb->posts} as p ,".
			"{$wpdb->term_relationships} as tr ,"."{$wpdb->term_taxonomy} as tt ,"
			."{$wpdb->terms} as t ".
			"where p.post_status='publish' ".
			"and p.id=tr.term_taxonomy_id ".
			"and tt.term_taxonomy_id=tr.term_taxonomy_id ".
			"and t.term_id=tt.term_id ".
			"order by p.id desc limit 0,5";
		*/
		$sql = "SELECT p.post_title,p.guid from {$wpdb->posts} p ".
			"where p.post_status='publish' ".
			"and 1=1 ".
			//关键字处
			"and ((p.post_title like '%{$k}%') ".
			"or (p.post_content like '%{$k}%'))".
			"order by p.id desc ";

		if($sign=='?'){
			$sql_num = $sql.$limit;
			$res = $wpdb->get_results($sql_num);
		}else{
			$num = $wpdb->get_results($sql);
			//var_dump($num);
			$p = ceil(count($num)/5);
			//var_dump($p);
			if($sign>$p){
				return false;
			}
			$sql_num = $sql.$limit;
			$res = $wpdb->get_results($sql_num);
		}
		return $res;
	}	
}
?>
